package main;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Observable;

//TODO: timer stuff and statistics stuff
public class PageAnalyzer extends Observable {
	//TODO: a lot of these will probably be passed in from the controller
	private int myPageCount; //counter for pages analyzed
	private int myTotalWords; //counter for word count
//	private int myAvgURLsPerPage; //avg links per each page
//	private long myAvgParseTimePerPage; //avg parse time per page
	private long  myTotalRunningTime; //count running time for all analyzations
	//following two are parallel array for keywords and count per keyword
	private String[] myKeywords; //holds all keywords
	private int[] myKeywordTally; //counts keyword count for each keyword
	private int numKeywords; //the number of keywords inputted by user (max = 10)
	private int myKeywordCount; //count all keywords
	private int maxPages; //max pages to parse
	private int myTotalLinks; //to calc avg urls per page
	
	/**
	 * Constructor. Create an empty instance of a PageAnalyzer object.
	 */
	public PageAnalyzer() {
		myPageCount = 0;
		myTotalWords = 0;
//		myAvgURLsPerPage = 0;
//		myAvgParseTimePerPage = 0;
		myTotalRunningTime = 0;
		myKeywordCount = 0;
		myKeywords = new String[10];
		myKeywordTally = new int[10];
		myTotalLinks = 0;
	}
	
	/**
	 * Populates keywords array and total pages.
	 * @param theKeywords
	 * @param numPages
	 */
	public void setUp(final List<String> theKeywords, final int numPages) {
		numKeywords = theKeywords.size();
		for (int i = 0; i < numKeywords; i++) {
			myKeywords[i] = new String(theKeywords.get(i));
		}
		maxPages = numPages;
	}
	
	/**
	 * Analyzes page and tabulates statistics.
	 * @param theWords	The collection of words in the page to analyze.
	 */
	public void analyzePage(final Page thePage) {
		List<String> theWords = thePage.getWords();
		//TODO: start timer (don't know how to do it threadsafe - I think we need to use the synchronize method.... - Allison)
//        // timer variables
//        long startTime = 0;
//        long finishTime = 0;

//        // start the timer
//        Date startDate = new Date();
//        startTime = startDate.getTime();

		System.out.println("analyzing " + thePage.getURL() + "..............");
		//for each word in the page, check against the keywords
		//count word and update specific keyword count
		for (String word : theWords) {
			myTotalWords++;
			for (int i = 0; i < numKeywords; i++) {
				if (word.equalsIgnoreCase(myKeywords[i])) {
					myKeywordTally[i]++;
				}
			}
//			System.out.println(word);
		}
		
//        // stop the timer
//        Date finishDate = new Date();
//        finishTime = finishDate.getTime();
//        myTotalRunningTime += (finishTime - startTime);
		myTotalRunningTime += thePage.getParseTime();

		myPageCount++;
		myTotalLinks+=thePage.getLinks().size();
				
		System.out.println("analization complete. Pages counted: " + myPageCount + ", total words: " + myTotalWords + ", total links: " + myTotalLinks);
		if(myPageCount >= maxPages) {
			tabulateStatistics();
		}
	}
	
	public int getPagesAnalyzed() {
		return myPageCount;
	}
	
	public int getMaxPages() {
		return maxPages;
	}
	
	public void tabulateStatistics() {
		List<KeywordStatisticsContainer> kst = new ArrayList<KeywordStatisticsContainer>();
		for (int i = 0; i < numKeywords; i++) {
			KeywordStatisticsContainer kw = new KeywordStatisticsContainer(myKeywords[i], myKeywordTally[i], myKeywordTally[i]/(double)myPageCount);
			kst.add(kw);
		}
		StatisticsContainer stats = new StatisticsContainer(myPageCount, myTotalWords/myPageCount, myTotalLinks/myPageCount, kst, myTotalRunningTime/myPageCount, myTotalRunningTime);
		//TODO: avg URLs per page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		
		setChanged();
		notifyObservers(stats);
	}
}
